/*
	The MIT License

	Copyright (c) 2008 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/
// d3d_main.cpp - 

#include "d3d_local.h"


ED3DRenderSystem	*rs		=	NULL;
IShell				*shell	=	NULL;
IConsole			*con	=	NULL;
IFileSystem			*fs		=	NULL;


IRenderSystem *CreateRenderSystem(const ap_t &ap)
{
	return new ED3DRenderSystem(ap);
}

void DestroyRenderSystem(IRenderSystem *rs)
{
	delete rs;
}

/*-----------------------------------------------------------------------------
	
-----------------------------------------------------------------------------*/

//
//	ED3DRenderSystem::ED3DRenderSystem
//
ED3DRenderSystem::ED3DRenderSystem(const ap_t &ap)
{
	this->ap = ap;
	::rs = this;
	shell	=	ap.shell;
	fs		=	ap.fs;
	con		=	ap.con;
	
	LOG_SPLIT("Direct3D rendering system initialization");
	
	shell->AddCommand("listDisplayModes",	(EShellCmd_f)ListDisplayModes_f,	this);
	shell->AddCommand("screenShot",			(EShellCmd_f)ScreenShot_f,	this);
	
	InitDisplay();
	InitDirect3D();
	
	HRCALL(d3ddev->GetRenderTarget(0, &default_color_surf));
	HRCALL(d3ddev->GetDepthStencilSurface(&default_depth_surf));

	//cuda_test		=	new ECudaTest(rs);	
	cuda_test_mri	=	new ECudaTestMRI(rs);
	
	LOG_SPLIT("");
	
	color_2d_multiplier = EVec4(1,1,1,1);

	need_screenshot = false;
}


//
//	ED3DRenderSystem::~ED3DRenderSystem
//
ED3DRenderSystem::~ED3DRenderSystem(void)
{
	LOG_SPLIT("Direct3D rendering system shutting down");
	
	delete cuda_test_mri;
	//delete cuda_test;
	
	ReleaseImages();
	
	SAFE_RELEASE(default_color_surf);
	SAFE_RELEASE(default_depth_surf);
	
	ShutdownDirect3D();
	ShutdownDisplay();
	
	shell->RemoveCommand("screenShot");
	shell->RemoveCommand("listDisplayModes");
	
	LOG_SPLIT("");
}


/*-----------------------------------------------------------------------------
	Resource registering stuff :
-----------------------------------------------------------------------------*/

//
//	ED3DRenderSystem::BeginRegistration
//
void ED3DRenderSystem::BeginRegistration(const char *mapname)
{
	LOG_SPLIT(va("Loading : %s", mapname));
	
	ReleaseImages();
}


//
//	ED3DRenderSystem::EndRegistration
//
void ED3DRenderSystem::EndRegistration(void)
{
	LOG_SPLIT("");
}



/*-----------------------------------------------------------------------------
	Frame rendering stuff
-----------------------------------------------------------------------------*/

//
//	ED3DRenderSystem::ResetDeviceState
//
void ED3DRenderSystem::ResetDeviceState(void)
{	
	float zero  = 0;
	SetRenderTarget(NULL);
	HRCALL( d3ddev->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID) );
	HRCALL( d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE) );
	HRCALL( d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE) );
	HRCALL( d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE) );
	HRCALL( d3ddev->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE) );
	HRCALL( d3ddev->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESSEQUAL) );
	HRCALL( d3ddev->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *(int*)&zero) );
	HRCALL( d3ddev->SetRenderState(D3DRS_DEPTHBIAS, *(int*)&zero) );
	HRCALL( d3ddev->SetRenderState(D3DRS_ZWRITEENABLE, D3DZB_TRUE) );
	uint mask =	 D3DCOLORWRITEENABLE_ALPHA|D3DCOLORWRITEENABLE_BLUE|D3DCOLORWRITEENABLE_GREEN|D3DCOLORWRITEENABLE_RED;
	HRCALL( d3ddev->SetRenderState( D3DRS_COLORWRITEENABLE, mask ) );
	HRCALL( d3ddev->SetRenderState( D3DRS_ZWRITEENABLE, TRUE ) );
	HRCALL( d3ddev->SetRenderState( D3DRS_ZENABLE, TRUE ) );
	HRCALL( d3ddev->SetRenderState( D3DRS_STENCILENABLE, FALSE ) );

	for (uint i=0; i<8; i++) {
		LPDIRECT3DDEVICE9 d3ddev = rs->GetD3DDevice();
		HRCALL( d3ddev->SetTexture(i, NULL) );
		HRCALL( d3ddev->SetSamplerState(i, D3DSAMP_MAXANISOTROPY, 16) );
		HRCALL( d3ddev->SetSamplerState(i, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC) );
		HRCALL( d3ddev->SetSamplerState(i, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR) );
		HRCALL( d3ddev->SetSamplerState(i, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR) );
		
		SetTextureAddrMode(i, RS_TEXADDR_WRAP);
	}
	
	SetViewport(0,0,0,0);
	//FlushImageCache();
	
	/*shader_system->SetPixelShader(NULL);
	shader_system->SetVertexShader(NULL);*/
}


//
//	ED3DRenderSystem::BeginFrame
//
void ED3DRenderSystem::BeginFrame(void)
{
	int clear_flags = D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER|D3DCLEAR_STENCIL;
	HRCALL( d3ddev->Clear(0, NULL, clear_flags, D3DCOLOR_ARGB(0x00,0xF0,0x00,0x80), 1.0f, 0 ) );	

    HRCALL( d3ddev->BeginScene() );

	ResetDeviceState();
}


//
//	ED3DRenderSystem::RenderFrame
//
void ED3DRenderSystem::RenderFrame(void)
{
	uint w, h;

	Set2DViewMode();

	GetScreenSize( w, h );
	
	//cuda_test->Run();
	cuda_test_mri->Run();

	d3ddev->SetTexture(0, 0);							  
}


//
//	ED3DRenderSystem::EndFrame
//
void ED3DRenderSystem::EndFrame(void)
{
	ScreenShot();
	
	HRCALL( d3ddev->EndScene() );
    HRCALL( d3ddev->Present( NULL, NULL, NULL, NULL ) );
}


//
//	ED3DRenderSystem::SetWriteMask
//
void ED3DRenderSystem::SetWriteMask( bool rgb, bool a )
{
	uint mask =	 0;
	if (rgb) {
		mask |= ( D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_BLUE | D3DCOLORWRITEENABLE_GREEN );
	}
	if (a) {
		mask |= D3DCOLORWRITEENABLE_ALPHA;
	}
	HRCALL( d3ddev->SetRenderState( D3DRS_COLORWRITEENABLE, mask ) );
}


//
//	ED3DRenderSystem::ClearRenderTarget
//
void ED3DRenderSystem::ClearRenderTarget(void)
{
	int clear_flags = D3DCLEAR_TARGET;
	HRCALL( d3ddev->Clear(0, NULL, clear_flags, D3DCOLOR_ARGB(0x00,0x00,0x00,0x00), 1.0f, 0 ) );	
}


//
//	ED3DRenderSystem::SetRenderTarget
//
void ED3DRenderSystem::SetRenderTarget(IImage *render_target)
{
	if (!render_target) {
		HRCALL( d3ddev->SetRenderTarget(0, default_color_surf) );
		HRCALL( d3ddev->SetDepthStencilSurface(default_depth_surf) );
		return;
	}

	ED3DImage *d3d_tex = static_cast<ED3DImage*>(render_target);
	
	HRCALL( d3ddev->SetDepthStencilSurface(NULL) );
	HRCALL( d3ddev->SetRenderTarget(0, d3d_tex->Surface()) );
}












